package cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.business;

import cn.schoolwow.ams.domain.option.QuickAMSOption;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.domain.ScheduleJobFlow;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.cases.service.CleanFlowLogScheduleJobFlow;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.domain.ScheduleJobContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.domain.query.queryPackage.QueryPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.support.CronExpression;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetScheduleJobContextListFlow implements BusinessFlow {
    private Logger logger = LoggerFactory.getLogger(GetScheduleJobContextListFlow.class);

    private static final List<ScheduleJobContext> scheduleJobContextList = new ArrayList<>();

    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
        QuickAMSOption quickAMSOption = applicationContext.getBean(QuickAMSOption.class);

        flowContext.putTemporaryData("list", scheduleJobContextList);

        List<ScheduleJobFlow> scheduleJobFlowList = new ArrayList<>();
        scheduleJobFlowList.add(new CleanFlowLogScheduleJobFlow());
        List<ScheduleJobFlow> list = QueryPackage.newQuery(flowMonitorOption.basePackageName)
                .addClassQuery(ScheduleJobFlow.class)
                .execute()
                .getInstanceList(ScheduleJobFlow.class);
        if(null!=list&&!list.isEmpty()){
            scheduleJobFlowList.addAll(list);
        }

        for(ScheduleJobFlow scheduleJobFlow:scheduleJobFlowList){
            String categoryName = "其他";
            for(Map.Entry<String,String> entry:quickAMSOption.packageNameLabelMap.entrySet()){
                String className = scheduleJobFlow.getClass().getName();
                if(className.contains(entry.getKey())){
                    categoryName = entry.getValue();
                }
            }

            long count = QueryInstanceList.newQuery(scheduleJobContextList)
                    .addQuery("categoryName", categoryName)
                    .addQuery("name", scheduleJobFlow.name())
                    .execute()
                    .count();
            if(count>0){
                continue;
            }

            ScheduleJobContext scheduleJobContext = new ScheduleJobContext();
            scheduleJobContext.name = scheduleJobFlow.name();
            scheduleJobContext.cron = scheduleJobFlow.cron();
            //检查CRON表达式,不合法的话去掉
            if(!CronExpression.isValidExpression(scheduleJobContext.cron)){
                logger.warn("加载定时任务失败!不合法的CRON表达式!定时任务名称:"+scheduleJobContext.name);
                continue;
            }
            scheduleJobContext.categoryName = categoryName;
            scheduleJobContext.scheduleJobFlow = scheduleJobFlow;
            synchronized (scheduleJobContextList){
                scheduleJobContextList.add(scheduleJobContext);
            }
        }
    }

    @Override
    public String name() {
        return "获取定时任务上下文记录列表";
    }
}
